import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_shop_z/config/service_url.dart';
import 'package:flutter_shop_z/model/commodity.dart';
import 'package:flutter_shop_z/pages/cart_page.dart';
import 'package:flutter_shop_z/pages/details_page/details_bottom.dart';
import 'package:flutter_shop_z/pages/details_page/details_explain.dart';
import 'package:flutter_shop_z/pages/details_page/details_tab_bar.dart';
import 'package:flutter_shop_z/pages/details_page/details_top_area.dart';
import 'package:flutter_shop_z/service/service_method.dart';

class DetailsPage extends StatefulWidget {
  final int commodityId;
  DetailsPage(this.commodityId);
  _DetailsPageState createState() => _DetailsPageState(this.commodityId);
}

class _DetailsPageState extends State<DetailsPage> {
  final int commodityId;
  Commodity commodity;

  @override
  void initState() {
    _getCommodityById();
    super.initState();
  }

  _DetailsPageState(this.commodityId);
  @override
  Widget build(BuildContext context) {
    // _getCommodityById();
    return Scaffold(
      appBar: AppBar(
        title: Text(
          (commodity == null ? '商品' : commodity.commodityName),
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
        ),
      ),
      body: commodity == null
          ? Center(
              child: Text('暂无数据'),
            )
          : Stack(
              children: <Widget>[
                ListView(
                  children: <Widget>[
                    DetailsTopArea(commodity),
                    DetailsExplain(),
                    DetailsTabBar(commodity.categoryId),
                    Container(height: ScreenUtil().setHeight(80))
                  ],
                ),
                Positioned(
                  child: DetailsBottom(commodity),
                  bottom: 0,
                  left: 0,
                ),
              ],
            ),
    );
  }

  void _getCommodityById() {
    if (commodityId > 0) {
      var formData = {'commodityId': commodityId};
      getRequest(servicePath['commodityById'], formData).then((value) {
        setState(() {
          commodity = Commodity.fromJson(value['data']);
        });
      });
    }
  }
}
